package com.example.websocket.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.websocket.Service.UserService;
import com.example.websocket.pojo.Result;
import com.example.websocket.pojo.User;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sun.deploy.net.HttpResponse;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.*;

import java.io.IOException;
import java.io.PrintWriter;

import static java.awt.SystemColor.window;

@RestController
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping("/login")//登陆验证，账号密码正确则返回true
    public Result login(@RequestParam("username") String username, @RequestParam("pwd") String pwd, HttpSession session){
        Result result = new Result();
        result = userService.login(username,pwd);
        if(result.isFlag()){
            session.setAttribute("username",username);
        }
        return result;
    }



    @RequestMapping("/getUsername")
    public String GetUsername(HttpSession session){
        String username = (String) session.getAttribute("username");
        return username;
    }

    @RequestMapping ("/addfriend")
    public R addfriend(String Fname, HttpSession session) throws IOException {
        String username = (String) session.getAttribute("username");
        R result = new R();
        result = userService.AddFriend(username,Fname);
        return result;
    }

    @RequestMapping ("/delfriend")
    public R delfriend(String Dname, HttpSession session) throws IOException {
        String username = (String) session.getAttribute("username");
        R result = new R();
        result = userService.DelFriend(username,Dname);
        return result;
    }

}
